package com.doit.day03.不可变集合

object _12_groupBy {
  def main(args: Array[String]): Unit = {

    val arr: List[Int] = List(1, 2, 3, 4, 5)
    /*   val iterator: Iterator[Array[Int]] = arr.grouped(3)
       while (iterator.hasNext){
         val ints: Array[Int] = iterator.next()
         println(ints.toList)
       }*/
    //只要是groupBy了那么就会得到一个Map类型
    val booleanToInts: Map[Boolean, List[Int]] = arr.groupBy((a: Int) => {
      a > 3
    })
    val intToInts: Map[Int, List[Int]] = arr.groupBy(a => a % 2)


    val list = List("hello","hello","hi","hi","hanhan","hanhan","siyi","hello")
    //hello,List(hello,hello,hello)
    //hi,List(hi,hi)
    //hanhan,List(hanhan,hanhan)
    //siyi,List(siyi)
    val stringToStrings: Map[String, List[String]] = list.groupBy(word => word)
    println(stringToStrings)


    val list1 = List((1, "zss"), (2, "zss"), (3, "lss"), (3, "ww"), (2, "lss"), (3, "zss"))

    /*
        Map[String,List[(Int,String)]]
       zss List((1,zss), (2,zss), (3,zss))
       lss List((3,lss),(2,lss))
       ww List((3,ww))
     */
    val res: Map[String, List[(Int, String)]] = list1.groupBy(_._2)
    println(res)

    val stus = List(
      Student(1,"zss",18,"male"),
      Student(2,"zss",28,"male"),
      Student(3,"zss",38,"female"),
      Student(4,"zss",48,"male"),
      Student(5,"zss",58,"female"),
    )

    //类型 Map[String,List[Student]]
    println(stus.groupBy(_.gender))

  }
}
